package com.cty.service.impl;

import com.cty.commons.utils.JsonUtils;
import com.cty.dao.JedisClusterDao;
import com.cty.mapper.ImgMapper;
import com.cty.pojo.Img;
import com.cty.pojo.ImgExample;
import com.cty.service.ImgService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: cty
 * @Date: 2020/4/11 19:14
 * @Description:
 * @version: 1.0
 */
@Service
public class ImgServiceImpl implements ImgService {
    Logger log = Logger.getLogger(ImgServiceImpl.class);

    @Value("${bigpic.key}")
    private String key;
    @Resource
    private ImgMapper imgMapper;
    @Resource
    private JedisClusterDao jedisClusterDaoImpl;

    @Override
    public List<Img> selAll() {
//        // 判断redis中是否存在指定key
//        if(jedisClusterDaoImpl.exists("bigImg")){
//            // 若存在取出后判断是否为null或空字符串
//            String value = jedisClusterDaoImpl.get("bigImg");
//            if(value!=null && !value.equals("")){
//                log.info("从redis缓存中获取图片路径："+value);
//                return JsonUtils.jsonToList(value, Img.class);
//            }
//        }
//        // 如果不存在，从mysql中取出

        /* 在后台管理系统，只从mysql数据库中查询数据！而不从redis缓存中查询。 */
        // 查询体
        ImgExample example = new ImgExample();
//        // 条件查询
//        example.createCriteria().andIdEqualTo(1).andPathEqualTo("");
        List<Img> list = imgMapper.selectByExample(example);

        // 把缓存结果存到redis中
        jedisClusterDaoImpl.set("bigImg", JsonUtils.objectToJson(list));
        log.info("从mysql数据库中获取图片路径："+list);

        return list;
    }

    @Override
    public int delById(int id) {
        // 1 删除数据库中的数据
        ImgExample example = new ImgExample();
        example.createCriteria().andIdEqualTo(id);
        int index = imgMapper.deleteByExample(example);
        if(index > 0){
            log.debug("已经删除id="+id+"的图片");
            // 2 判断redis中是否存在缓存数据
            if(jedisClusterDaoImpl.exists(key)){
                String value = jedisClusterDaoImpl.get(key);
                // 3 若存在则修改redis中的缓存数据
                if(value!=null && !value.equals("")){
                    List<Img> list = JsonUtils.jsonToList(value, Img.class);

                    Img imgExists = null;
                    for(Img img:list){  // 列表在遍历时不能直接删除元素
                        if(img.getId()==id){
                            imgExists = img;
                        }
                    }
                    if(imgExists!=null){
                        list.remove(imgExists);
                    }
                    jedisClusterDaoImpl.set(key, JsonUtils.objectToJson(list));
                    log.debug("已同步redis中的缓存数据");
                }
            }

        }

        return index;
    }
}

/* res:
DEBUG-2020/04/11 09:15:51-org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.debug(JakartaCommonsLoggingImpl.java:49)-[http-nio-8888-exec-5]==>  Preparing: select id, path from img
DEBUG-2020/04/11 09:15:51-org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.debug(JakartaCommonsLoggingImpl.java:49)-[http-nio-8888-exec-5]==> Parameters:
DEBUG-2020/04/11 09:15:51-org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.debug(JakartaCommonsLoggingImpl.java:49)-[http-nio-8888-exec-5]<==      Total: 2
 INFO-2020/04/11 09:15:51-com.cty.service.impl.ImgServiceImpl.selAll(ImgServiceImpl.java:51)-[http-nio-8888-exec-5]从mysql数据库中获取图片路径：[Img{id=1, path='http://192.168.238.128/cf6d6cfa-070d-43d6-9d85-f4ba9904ca6a.png'}, Img{id=2, path='http://192.168.238.128/d209a0dd-2f74-4a9a-a2ae-e4fcfb173f42.jpeg'}]
 INFO-2020/04/11 09:16:11-com.cty.service.impl.ImgServiceImpl.selAll(ImgServiceImpl.java:37)-[http-nio-8888-exec-7]从redis缓存中获取图片路径：[{"id":1,"path":"http://192.168.238.128/cf6d6cfa-070d-43d6-9d85-f4ba9904ca6a.png"},{"id":2,"path":"http://192.168.238.128/d209a0dd-2f74-4a9a-a2ae-e4fcfb173f42.jpeg"}]
*/
